#ifndef __USR_PROTOCOL_H
#define __USR_PROTOCOL_H
//#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif



#define MAX_ID 100
#define MAX_MOTOR_REG 1106
#define MAX_BOARD_PARAM_REG 160


#define BROADCAST_CONTROL_REG 0u
#define BROADCAST_SETPOSITION_CMD 401u
#define BROADCAST_CONTROL_CMD 501u
#define BROADCAST_POSITIVE_TORQUE 1103u
#define BROADCAST_NEGATIVE_TORQUE 1104u
#define CONTROL_BOARD_STATUS 1105u

#define CONTROL_BOARD_PARAM_BASE 2000u
#define BROADCAST_IP_SAVE 2001u
#define BROADCAST_IP_1  2010u
#define BROADCAST_IP_2  2011u
#define BROADCAST_IP_3  2012u
#define BROADCAST_IP_4  2013u
#define BROADCAST_MAX_SPEED 2027u
#define BROADCAST_MOTOR_STATUS_BASE 2030u



#define CONTROL_BOARD_DEVICE_NUM 2140u

#define SYS_ERRO_BASE 130u
#define COMM_ERROR_BASE 131u
#define MOTOR_ERROR_BASE 132u

#define MAX_ID_ADDR 140u

#define MOTOR_SETPOINT_POSTION_BASE 401u


// Add these defines at the top of the file
#define MOTOR_ERROR_BIT      0
#define MOTOR_ERROR_MASK    (1 << MOTOR_ERROR_BIT)
#define MOTOR_STATUS_BIT     1
#define MOTOR_STATUS_MASK    (1 << MOTOR_STATUS_BIT)
#define MOTOR_TEST_UP_DOWN_LIMIT_BIT     2
#define MOTOR_TEST_UP_DOWN_LIMIT_MASK    (1 << MOTOR_TEST_UP_DOWN_LIMIT_BIT)
#define MOTOR_TEST_UP_DOWN_LIMIT_DONE_BIT     3
#define MOTOR_TEST_UP_DOWN_LIMIT_DONE_MASK    (1 << MOTOR_TEST_UP_DOWN_LIMIT_DONE_BIT)
#define MOTOR_TEST_UP_LIMIT_DONE_BIT     6
#define MOTOR_TEST_UP_LIMIT_DONE_MASK    (1 << MOTOR_TEST_UP_LIMIT_DONE_BIT)
#define MOTOR_TEST_DOWN_LIMIT_DONE_BIT     7
#define MOTOR_TEST_DOWN_LIMIT_DONE_MASK    (1 << MOTOR_TEST_DOWN_LIMIT_DONE_BIT)



// 添加到头文件中
typedef struct {
    int16_t channel;
    int16_t reg_addr;
    int16_t reg_value;
  } ModbusCommand_t;





extern int16_t *motor_actual_positon_base_addr[2];
extern int16_t *motor_actual_erro_base_addr[2] ;
extern int16_t *motor_actual_temp_base_addr[2] ;
extern int16_t *motor_actual_current_base_addr[2] ;
extern int16_t *motor_setpoint_positon_base_addr[2] ;
extern int16_t *motor_control_reg_base_addr[2] ;
extern int16_t *motor_status_reg_base_addr[2] ;
extern int16_t *motor_uplimit_positon_base_addr[2] ;
extern int16_t *motor_downlimit_position_base_addr[2];
extern int16_t *motor_max_record_current_base_addr[2];
extern int16_t *motor_max_record_temp_base_addr[2];
extern int16_t *control_board_base_addr[2];
extern int16_t *device_online_status_base_addr[2];
extern int16_t*sys_erro[2];
extern int16_t *comm_error[2];
extern int16_t *motor_err[2];

// 引用外部变量
extern int16_t motor_status_reg_map[2][1106];
extern int16_t control_board_param_reg_map[2][MAX_BOARD_PARAM_REG];



#define BITS_PER_ELEMENT 32
#define BIT_ARRAY_SIZE ((MAX_ID + BITS_PER_ELEMENT - 1) / BITS_PER_ELEMENT)

// 位数组定义
extern uint32_t motor_position_flags[BIT_ARRAY_SIZE]; // 需要 (100+31)/32 = 4 个32位整数 = 16字节

void setFlag(uint32_t* array, uint16_t pos);
void clearFlag(uint32_t* array, uint16_t pos);
uint8_t checkFlag(uint32_t* array, uint16_t pos);
void clearAllFlags(uint32_t* array, uint16_t size);

void send_modbus_command(int16_t addr, int16_t value,int channel);
uint8_t process_modbus_to_motor(uint16_t reg_addr, int16_t reg_value, uint8_t motor_group);

void init_control_board(void);

void update_motor_status(uint8_t uart_channel, uint8_t id, uint16_t motor_status);
void update_motor_error_status(uint8_t uart_channel, uint8_t id, int16_t motor_error);
void update_motor_testing_up_down_limit(uint8_t uart_channel, uint8_t id, uint16_t motor_status);
void update_motor_testing_up_down_limit_done(uint8_t uart_channel, uint8_t id, uint16_t motor_status);
void update_motor_up_limit_reached(uint8_t uart_channel, uint8_t id, uint16_t motor_status);
void update_motor_down_limit_reached(uint8_t uart_channel, uint8_t id, uint16_t motor_status);





#ifdef __cplusplus
}
#endif

#endif// __MAIN_H